/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmiranda.afd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import sagex.api.*;

/**
 *
 * @author Tom Miranda
 */
public class api {
    
    // Returns true if the Airing (or Show) is an anti-favorite, false otherwise.
    public static boolean isAntiFavorite(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.isAntiFavorite();
    }
    
    // Makes the Airing an anti-favorite and sets all future Airings to Watched and DontLike.
    public static boolean setAntiFavorite(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.setAntiFavorite();
    }
    
    // Clears (sets to null) the anti-favorite status or an Airing.
    public static boolean clearAntiFavorite(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.clearAntiFavorite();
    }   
    
    // Completely removes the UserRecord for the Airing or Show.
    public static boolean removeAntiFavorite(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.removeAntiFavorite();
    }
    
    public static boolean setMarkWatched(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.setMarkWatched();
    }
    
    public static boolean clearMarkWatched(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.clearMarkWatched();
    }
    
    public static boolean isMarkWatched(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.isMarkWatched();
    }
    
    public static boolean setMarkDontLike(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.setMarkDontLike();
    }
    
    public static boolean clearMarkDontLike(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.clearMarkDontLike();
    }
    
    public static boolean isMarkDontLike(Object Airing) {
        AntiFavorite af = new AntiFavorite(Airing);
        return af.isMarkDontLike();
    }
    
    // Deletes the entire UserRecord datastore.
    public static void removeAllAntiFavorites() {
        Log.getInstance().write(Log.LOGLEVEL_WARN, "removeAllAntiFavorites: DataStore has been completely removed.");
        UserRecordAPI.DeleteAllUserRecords(DataStore.STORE);
    }
    
    // Returns a List of Shows.
    public static List<Object> getAllAntiFavorites() {
        Object[] allRecords = UserRecordAPI.GetAllUserRecords(DataStore.STORE);
        Log.getInstance().write(Log.LOGLEVEL_TRACE, "getAllAntiFavorites: Records found " + allRecords.length);
        
        ArrayList<Object> list = new ArrayList<Object>();
        
        if (allRecords==null || allRecords.length==0)
            return list;
        
        for (Object record : allRecords) {
            String externalID = UserRecordAPI.GetUserRecordData(record, DataStore.NAME_ID);
            String status = UserRecordAPI.GetUserRecordData(record, DataStore.NAME_FAV);
            Log.getInstance().write(Log.LOGLEVEL_TRACE, "getAllAntiFavorites: Record data " + externalID + " '" + status + "'" + " " + ShowAPI.GetShowForExternalID(externalID));
            
            if (externalID!=null && status!=null && status.equalsIgnoreCase("true")) {
                Object show = ShowAPI.GetShowForExternalID(externalID);
                
                if (show != null) {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "getAllAntiFavorites: Found Show");
                    list.add(show);
                } else {
                    Log.getInstance().write(Log.LOGLEVEL_TRACE, "getAllAntiFavorites: null Show");
                }   
            }
        }
        
        return list;
    }
    
    public static void dumpDatabase() {
        List<Object> allAntiFavorites = getAllAntiFavorites();
        System.out.println("AFD: dumpingDatabase: Found antiFavorites = " + allAntiFavorites.size());
        
        for (Object favorite : allAntiFavorites) {
            DataStore ds = new DataStore(favorite);
            System.out.format("AFD: Title=%s, ID=%s, isFavorite=%s, markWatched=%s, markDontLike=%s", ds.getTitle(), ds.getID(), ds.getAntiFavoriteStatus(), ds.getMarkWatchedStatus(), ds.getMarkDontLikeStatus());
        }
    }
 
}
